package org.dream.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.dream.domain.Industry;
import org.dream.service.StockService;
import org.dream.service.StockServiceImpl;

/**
 * 股票列表面板
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class IndustryTablePanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTable stockTable;

	private Vector<String> columnNames;
	private StockService stockService = new StockServiceImpl();
	private  boolean calculate = false;
	public IndustryTablePanel( boolean calculate) {
		super(new BorderLayout());
		this.calculate=calculate;
		addColumnNames();// 添加表格列名称
		// addStockKindBox(); // 添加股票分类Box
		addStockDataTable();// 初始化股票数据表格

	}

	/**
	 * 添加表格列名称
	 */
	private void addColumnNames() {
		columnNames = new Vector<String>();
		columnNames.add("序号");
		columnNames.add("日期");
		columnNames.add("板块名称");
		columnNames.add("均涨幅%");
		columnNames.add("权涨幅%");
		columnNames.add("总成交");
		columnNames.add("市场比%");
		columnNames.add("换手率%");
		columnNames.add("市盈(动)");
		columnNames.add("领涨股票");
		columnNames.add("涨股比");

	}

	/**
	 * 得到股票表格数据
	 */
	public Vector<Vector<Object>> getStockTableRowData() {
		Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		List<Industry> industryList = stockService.findIndustrys(calculate);

		for (int i = 0; i < industryList.size(); i++) {
			Vector<Object> row = new Vector<Object>();
			row.add("" + (i + 1));
			row.add(industryList.get(i).getDay());
			row.add(industryList.get(i).getBkmc());
			row.add(industryList.get(i).getJzf());
			row.add(industryList.get(i).getQzf());
			row.add(industryList.get(i).getZcj());
			row.add(industryList.get(i).getScb());
			row.add(industryList.get(i).getHsl());
			row.add(industryList.get(i).getSy_d());
			row.add(industryList.get(i).getLzgp());
			row.add(industryList.get(i).getZgb());
			data.add(row);
		}
		return data;
	}

	/**
	 * 初始化股票数据表格
	 */
	private void addStockDataTable() {

		stockTable = new JTable(getStockTableRowData(), columnNames);
		stockTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (stockTable.getSelectedRow() == -1) {
					return;
				}

			}
		});
		stockTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
		add(new JScrollPane(stockTable), BorderLayout.CENTER);
	}

	/**
	 * 更新数据表格数据
	 */
	private void refreshStockDataTable() {
		DefaultTableModel tableModel = new DefaultTableModel(getStockTableRowData(), columnNames);
		stockTable.setModel(tableModel);
		stockTable.updateUI();
	}
}
